$(document).ready(function()
{

	$('#completed').click(function()
	{
		if( $("input:checked[name='tcheckbox']").size()==0)
			{
				alert("Please select atleast one task");
				return false;
			}

		response=confirm("Are you sure you want to mark selected task(s) as complete");

		if(!response)
			return false;
		$('input[name="tcheckbox"]').each(function(index)
		{
			if($(this).attr("checked")==true)
			markComplete($(this).attr("id"));	
		});
	})


	$('#updateProgress').click(function()
	{
		if( $("input:checked[name='tcheckbox']").size()==0)
			{
				alert("Please select atleast one task");
				return false;
			}
		$('input[name="tcheckbox"]').each(function(index)
		{
		if($(this).attr("checked")==true)
			updateProgress($(this).attr("id"));	
		});
	})

	$('#searchString').keyup(function() 
	{
	 search();
	});

	$('#resetButton').click(function() 
	{
	 $('#searchString').val("");
	  search();
	});

});


function reset()
{
	tname= $("#taddName").val("");
	tdesc = $("#taddDesc").val("");
	assignee = $("#taddAssignee").val(0);
	priority = $("#taddPriority").val(0);
	module = $("#taddModule").val(0);
	$("#tmsg").css("visibility","hidden").css("opacity",1);
	//deadline = $("#taddDeadline").val();
}

function markComplete(id)
{

	$.ajax({
	url: "page.php",
	type: "POST",
	data: "completed="+id,
	success: function(html){

		if(html==1)
		{
			$("#status"+id).hide().html("COMPLETED").fadeIn('slow');
			$("#percentage"+id).hide().html('100').fadeIn('slow');
			count=$("input:checked[name='tcheckbox']").size()
			showMessage(count+" task(s) marked as complete");
		}
	}
	});

}

function showMessage(msg)
{
	$("#msg").html(msg).css("visibility","visible").animate({opacity:0},5500,function() 
	{

		$("#msg").css("visibility","hidden").css("opacity",1);	
	});
}



function selectProject(el)
{
	alert(el);
	id=el.name;

	$.ajax({
url: "page.php",
type: "POST",
data: "getProjectName="+id,
success: function(html){
	
	name=html;
		
	$.ajax({
url: "page.php",
type: "POST",
data: "getModules="+id,
success: function(modules){
	
$.ajax({
url: "page.php",
type: "POST",
data: "selectProject="+id,
success: function(html){
	
	$("#tablediv").html(html);
    $("#tasks").html(name);
	$("#projectid").val(id)
	$("#modulesDiv").html(modules)
}
});
}
});	
}
});
}




function sorty(el)
{
	var newSort = $(el).attr("name");
	var oldSort = $("#sort");
	var desc = $("#desc");
	var search = $("#searchString").val();

	if(newSort=="filters")
		{
			newSort=oldSort.val();
		}
	else
		{

			if(oldSort.val()==newSort)
			{
				if(desc.val()=="false")
					desc.val("true");
				else if(desc.val()=="true")
					desc.val("false");
			}
			else
				desc.val("false");
				oldSort.val(newSort);
		}
		var pid = $("#projectid").val();
		updateTasks(pid,newSort,desc.val(),search,"","","");
}


function search()
{
	var pid = $("#projectid").val();
	var search = $("#searchString").val();

	updateTasks(pid,"task_name","false",search,"","","");

}

function updateTasks(pid,sort,descc,search,module,status,priority)
{
	
	data="updateTasks="+pid+"&sort="+sort+"&desc="+descc+"&search="+search;
	data=data+getFilterString();

	$.ajax({
	url: "page.php",
	type: "POST",
	data: data,
	success: function(html)
		{
				$("#tablediv").html(html);
		}
	});
}



function getFilterString()
{
	var module = $("#module").val();
	var status = $("#status").val();
	var priority = $("#priority").val();

	if(module=="--MODULES--")
		module="";
	if(status=="--STATUS--")
		status="";
	if(priority=="--PRIORITY--")
		priority="";

	var string="&module="+module+"&status="+status+"&priority="+priority;

	return string;

}








function centerPopup(popupName){  

//request data for centering  
var windowWidth = document.documentElement.clientWidth;  
var windowHeight = document.documentElement.clientHeight;  
var popupHeight = $("#"+popupName).height();  
var popupWidth = $("#"+popupName).width();  
//centering  
$("#"+popupName).css({  
"position": "absolute",  
"top": windowHeight/2-popupHeight/2,  
"left": windowWidth/2-popupWidth/2  
});  
//only need force for IE6  
  
$("#background").css({  
"height": windowHeight  
});  
  
}  

function openPopup(popupName)
{
popupStatus = 0;  

if(popupStatus==0)
	{  
$("#background").css({"opacity": "0.6"});  
$("#background").fadeIn("slow");  
$("#"+popupName).fadeIn("slow");  
popupStatus = 1;  

centerPopup(popupName);

}
}




function closePopup(popupName)
{  
//disables popup only if it is enabled  
if(popupStatus==1){  
$("#background").fadeOut("slow");  
$("#"+popupName).fadeOut("slow");  
popupStatus = 0;  
}  
}  

function openTask(id)
{
	
$.ajax({
url: "page.php",
type: "POST",
data: "topen="+id,
success: function(html){
$("#status"+id).hide().html("OPEN").fadeIn('slow');
$("#percentage"+id).hide().html(html).fadeIn('slow');
showMessage("New task opened");
}
});
	
}





function showAddTask()
{

var pid = $("#projectid").val();
	openPopup('popupAddTask');
$.ajax({
url: "page.php",
type: "POST",
data: "addTaskTable="+pid,
success: function(html){
	
	$("#popupAddTask").html(html);	
		}
	});
}
function empty(ch)
{
	if(ch=="")
	{
		showTaskMsg("NAME OR DESCRIPTION CANNOT BE LEFT BLANK");
		return false;
	}
	else
		return true;
}

function pri(p)
{
	if(p=="--PRIORITY--"||p=="--USER--"||p=="--MODULES--")
	{
		showTaskMsg("SELECT "+p+"FROM LIST");
		return false;
	}
	else
		return true;
}
function addTask()
{
var tname= $("#taddName").val();
var tdesc = $("#taddDesc").val();
var assignee = $("#taddAssignee").val();
var priority = $("#taddPriority").val();
var module = $("#taddModule").val();
var deadline = $("#taddDeadline").val();

if(empty(tname)&&empty(tdesc)&&pri(assignee)&&pri(priority)&&pri(module))
{
var data= "addTask=&tname="+tname+"&tdesc="+tdesc+"&tassignee="+assignee+"&tpriority="+priority+"&tmodule="+module+"&tdeadline="+deadline;

	$.ajax({
url: "page.php",
type: "POST",
data: data,
success: function(html){

var pid = $("#projectid").val();
var desc = $("#desc").val();
var sort = $("#sort").val();

closePopup('popupAddTask');	
updateTasks(pid,sort,desc,"","","","");	
	
}
});
}
}

function showTaskMsg(msg)
{
	$("#tmsg").html(msg).css("visibility","visible");
}

function showReqExn()
{

var pid = $("#projectid").val();
	openPopup('popup_ex');
$.ajax({
url: "page.php",
type: "POST",
data: "requestExtnTable="+pid,
success: function(html){
	
	$("#popup_ex").html(html);

	
}
});
}
function edit()
{
	closePopup('profilePopup');
	openPopup('editProfilePopup');

}


function save()
{

var username= $("#editusername").val();
var firstname = $("#editfirstname").val();
var lastname = $("#editlastname").val();
var email = $("#editemail").val();

var data="save=&firstname="+firstname+"&lastname="+lastname+"&email="+email;

$.ajax({
url: "page.php",
type: "POST",
data: data,
success: function(html)
{
$("#profilePopup").html(html);
closePopup('editProfilePopup');	
showMessage("Profile edited");

}
});
}

function changePassword()
{
	closePopup('editProfilePopup');
	openPopup('changePasswordPopup');
}


function change()
{
	old=$("#oldpass").val();
	newp=$("#newpass").val();
	cnewp=$("#cnewpass").val();
	if(old==""||newp==""||cnewp=="")
	{
		alert("Enter all fields");
		$("#oldpass").val("");
		$("#newpass").val("");
		$("#cnewpass").val("");
		return false;
	}
	
	if(old==newp)
	{	
		alert("Please enter different password");
		$("#oldpass").val("");
		$("#newpass").val("");
		$("#cnewpass").val("");
		return false;
	}
	if(newp==cnewp)
	{
		
		var data="change=&old="+old+"&newp="+newp;
		$.ajax
		({
			url: "page.php",
			type: "POST",
			data:data,
			success: function(html)
			{
				if(html==1)
				{
					$("#oldpass").val("");
					$("#newpass").val("");
					$("#cnewpass").val("");
					closePopup('changePasswordPopup');
					showMessage("Password Changed");

				}
				else
					alert("Invalid password");
					$("#oldpass").val("");
					$("#newpass").val("");
					$("#cnewpass").val("");
			}
		});
	}   
	else
	{
		alert("Password not matching");
		$("#oldpass").val("");
		$("#newpass").val("");
		$("#cnewpass").val("");
	}
}
function 	updateProgress(id)
{
	
$.ajax({
url: "page.php",
type: "POST",
data: "update="+id,
success: function(html){
openPopup('updateProgressPopup');
$("#taskname").val(html);
$("#taskid").val(id);

}
})
	
} 
function progress()
{
	var percentage=$("#percentage").val();
	var id=$("#taskid").val();
	var data="progress=&percentage="+percentage+"&id="+id;
		
		$.ajax
		({
			url: "page.php",
			type: "POST",
			data: data,
			success: function(html)
			{
			
				$("#percentage"+id).hide().html(percentage).fadeIn('slow');
				if(percentage==100)
					$("#status"+id).hide().html("COMPLETED").fadeIn('slow');
				closePopup('updateProgressPopup');
				showMessage("Progress Updated");
				$("#percentage").val("");
			}
		})
}
